package com.six.controller;

import com.six.domain.dto.OrderCreateDTO;
import com.six.domain.dto.OrderQueryDTO;
import com.six.domain.dto.OrderUpdateDTO;
import com.six.domain.vo.MyOrderVO;
import com.six.service.OrderService;
import com.six.util.Result;   // 或 R，根据你的实际类名
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/order")
@RequiredArgsConstructor
public class OrderController {

    private final OrderService orderService;

    @PostMapping("/submit")
    public Result<Long> create(@RequestBody OrderCreateDTO dto) {
        return Result.success(orderService.createOrder(dto));
    }

    // 商家修改订单
    @PutMapping("/{orderId}")
    public Result<Void> updateOrder(@PathVariable Long orderId,
                                    @Validated @RequestBody OrderUpdateDTO dto) {
        orderService.updateOrder(orderId, dto);
        return Result.success(null);
    }
    @GetMapping("/my")
    public Result<List<MyOrderVO>> myOrders(@ModelAttribute OrderQueryDTO dto) {
        return Result.success(orderService.myOrders(dto));
    }

    @PostMapping("/{id}/pay")
    public Result<Void> pay(@PathVariable Long id,
                            @RequestParam("channel") String channel) {
        orderService.pay(id, channel);
        return Result.success(null);
    }

    @PostMapping("/{id}/cancel")
    public Result<Void> cancel(@PathVariable Long id) {
        orderService.cancel(id);
        return Result.success(null);
    }
    @GetMapping("/{id}")
    public Result<MyOrderVO> detail(@PathVariable Long id) {
        return Result.success(orderService.detail(id));
    }
    @PostMapping("/{id}/complete")
    public Result<Void> complete(@PathVariable Long id) {
        orderService.completeOrder(id);
        return Result.success(null);
    }
}